# -*- coding: utf-8 -*-
"""
Created on Mon Dec 03 13:47:48 2012

@author: Ago
"""
# Po změně QT Desinger - nahradit do ui_mainwindow3d.py 2řádky...
##--------------------------------------------------------------------------        
# ->       self.vtkWidget = QVTKRenderWindowInteractor(self.tab)
# ->       self.vtkWidget.setGeometry(QtCore.QRect(0, 0, 880, 840))        
##----------------------------------------------------------------------------        
##        self.graphicsView = QtGui.QGraphicsView(self.tab)
##        self.graphicsView.setGeometry(QtCore.QRect(0, -10, 961, 781))
##        self.graphicsView.setObjectName(_fromUtf8("graphicsView"))

import vtk

class Zobrazit3D():
    
    def __init__(self, instSp):
        
        self.instSp=instSp
        

        
    def zobraz3D(self, modelStl):
        
        self.modelStl=str(modelStl)        

        model = vtk.vtkSTLReader()
        model.SetFileName(self.modelStl)      
        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInputConnection(model.GetOutputPort())
        actor = vtk.vtkActor()
        actor.SetMapper(mapper)
	        
        surfaceFilter = vtk.vtkDataSetSurfaceFilter()
        surfaceFilter.SetInput(model.GetOutput())
        surfaceFilter.Update() 

        meshNormals = vtk.vtkPolyDataNormals()
        meshNormals.SetInput(surfaceFilter.GetOutput())
        
       # clip data
        self.plane = vtk.vtkPlane()
        self.clipper = vtk.vtkClipPolyData()
        self.clipper.SetInput(meshNormals.GetOutput())
        self.clipper.SetClipFunction(self.plane)
        self.clipper.GenerateClipScalarsOn()
        self.clipper.GenerateClippedOutputOn()
        self.clipper.SetValue(0.0)
        
        clipMapper = vtk.vtkPolyDataMapper()
        clipMapper.SetInputConnection(model.GetOutputPort())    
        clipMapper.ScalarVisibilityOff()
        
        backProp = vtk.vtkProperty()
        backProp.SetDiffuseColor((1.0,0.0,0.0))
        
        clipActor = vtk.vtkActor()
        clipActor.SetMapper(clipMapper)
        clipActor.GetProperty().SetColor((0.0,1.0,0.0))
        clipActor.SetBackfaceProperty(backProp)
        
        # cut mesh
        self.cutEdges = vtk.vtkCutter()
        self.cutEdges.SetInput(meshNormals.GetOutput())
        self.cutEdges.SetCutFunction(self.plane)
        self.cutEdges.GenerateCutScalarsOn()
        self.cutEdges.SetValue(0, 0.0)

        self.cutStrips = vtk.vtkStripper()
        self.cutStrips.SetInput(self.cutEdges.GetOutput())
        self.cutStrips.Update()

        self.cutPoly = vtk.vtkPolyData()
        self.cutPoly.SetPoints(self.cutStrips.GetOutput().GetPoints())
        self.cutPoly.SetPolys(self.cutStrips.GetOutput().GetLines())

        cutTriangles = vtk.vtkTriangleFilter()
        cutTriangles.SetInput(self.cutPoly)

        self.cutMapper = vtk.vtkPolyDataMapper()
        self.cutMapper.SetInput(cutTriangles.GetOutput())

        cutActor = vtk.vtkActor()
        cutActor.SetMapper(self.cutMapper)
        cutActor.GetProperty().SetColor((0.0,0.0,1.0))

        #Vytvořit grafické věci. Create graphics stuff
        self.ren = vtk.vtkRenderer()
        self.renWin = self.instSp.widget.GetRenderWindow()
        self.renWin.AddRenderer(self.ren)
        self.ren.AddViewProp(actor)
        
#        self.iren = vtk.vtkRenderWindowInteractor()
#        interactorStyle = vtk.vtkInteractorStyleTrackballCamera()
#        self.iren.SetInteractorStyle(interactorStyle)
#        self.iren.SetRenderWindow(self.renWin)
#        #Vytvořit rovina věci - Create plane stuff
#        planeWidget = vtk.vtkImplicitPlaneWidget()
#        planeWidget.SetNormalToZAxis(45)
#        planeWidget.DrawPlaneOff()
#        planeWidget.SetInteractor(self.iren)
#        planeWidget.SetPlaceFactor(1.5)
#        planeWidget.SetInput(meshNormals.GetOutput())
#        planeWidget.PlaceWidget()
#        planeWidget.AddObserver("InteractionEvent", self.myCallback)
#        planeWidget.On()

        #Přidat herce k renderer, nastavte pozadí a velikost - Add the actors to the renderer, set the background and size
        self.ren.AddActor(clipActor)
        self.ren.AddActor(cutActor)
        self.ren.SetBackground(0.0, 0.0, 0.0)
        self.ren.GetActiveCamera().Azimuth(90)
        self.ren.GetActiveCamera().Elevation(2)
        self.ren.GetActiveCamera().Dolly(0.007)
        self.ren.ResetCameraClippingRange()
        
        self.ren.ResetCamera()

       
#    def cut(self, v):
#        
#        self.clipper.SetValue(v)
#        self.cutEdges.SetValue(0, v)
#        self.cutStrips.Update()
#        self.cutPoly.SetPoints(self.cutStrips.GetOutput().GetPoints())
#        self.cutPoly.SetPolys(self.cutStrips.GetOutput().GetLines())
#        self.cutMapper.Update()
#        self.renWin.Render()
#
#        #Zpětné volání - callback function 
#    def myCallback(self, obj, event):
#        
#        global plane, selectActor
#        obj.GetPlane(self.plane)
#        self.cut(0.0)# Přiřaďte řádku widget s interactor - Associate the line widget with the interactor	
#        